﻿using System.Collections.Generic;

namespace DomainModel
{
    public class User : EntityBase
    {
        private readonly List<Role> _roles;

        public virtual string Login { get; protected set; }
        public virtual string PasswordHash { get; protected set; }
        public virtual string Email { get; protected set; }
        public virtual bool IsApproved { get; set; }
        public virtual Profile Profile { get; set; }
        public virtual Group Group { get; set; }
        public virtual Subgroup Subgroup { get; set; }
        public virtual User Child { get; set; } //для родителей
        public virtual IEnumerable<Role> Roles
        {
            get { return _roles; }
        }

        protected User()
        {
            _roles = new List<Role>();
        }

        public User(string login, string email)
            : this()
        {
            Login = login;
            Email = email;
            IsApproved = false;
        }

        public virtual void AddRole(Role role)
        {
            if (!_roles.Contains(role))
                _roles.Add(role);
        }

        public virtual void SetPassword(string password)
        {
            PasswordHash = GetPasswordHash(password);
        }

        public static string GetPasswordHash(string password)
        {
            return password.GetHashCode().ToString();
        }
    }
}
